package servlet;

import util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;

@WebServlet("/addEmp")
public class AddEmpServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        // 处理Post请求乱码
        req.setCharacterEncoding("utf-8");
        // 获取请求数据
        // req.getParameter(key)根据请求数据中的key获取与之对应的value
        // value即为用户在表单区域中输入的信息
        String name = req.getParameter("name");
        String salary = req.getParameter("salary");
        String gender = req.getParameter("gender");
        String edu = req.getParameter("edu");
        String[] hobbies = req.getParameterValues("hobbies");
        System.out.println("name:" + name + ",salary:" + salary + ",gender:" + gender + ",edu:" + edu + ",hobbies:" + Arrays.toString(hobbies));

        // JDBC操作
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/web?useUnicode=true&characterEncoding=utf-8", "root", "root");
            String sql = new StringBuffer()
                    .append(" insert into ")
                    .append(" t_emp ")
                    .append("   (name,salary,gender,edu,hobbies)")
                    .append(" values ")
                    .append("  (?,?,?,?,?)")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setDouble(2, Double.parseDouble(salary));
            ps.setString(3, gender);
            ps.setString(4, edu);
            ps.setString(5, StringUtil.arrayToString(hobbies));
            ps.executeUpdate();

            // 成功
//            out.println("<h1 style='color:green'>恭喜您,添加成功</h1>");
            // 当添加成功之后,页面跳转到员工列表
            // 在Servlet中,存在两种页面跳转方式
            // 1.转发
//            req.getRequestDispatcher("findAll").forward(req,resp);
//            req.getRequestDispatcher("/WEB-INF/test.jsp").forward(req,resp);
            // 2.重定向
//            resp.sendRedirect("findAll");
//            resp.sendRedirect("/WEB-INF/test.jsp");
            resp.sendRedirect(req.getContextPath()+"/findAll");
        } catch (ClassNotFoundException e) {
            out.println("<h1 style='color:red'>对不起,添加失败,失败原因:" + e.getMessage() + "</h1>");
        } catch (SQLException e) {
            out.println("<h1 style='color:red'>对不起,添加失败,失败原因:" + e.getMessage() + "</h1>");
        }

    }
}
